using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;

namespace RichImageField_ControlAdapter
{
    class RichImageFieldControlAdapterFeature : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            if (site == null) return;

            SPWebApplication webApplication = site.WebApplication;

            foreach (SPUrlZone zone in webApplication.IisSettings.Keys)
            {
                // The settings of the IIS application to update
                SPIisSettings settings = webApplication.IisSettings[zone];

                #region Copy to Microsoft.SharePoint.Publishing.dll assembly to the local webapplication folder
                try
                {
                    Type riftype = typeof(Microsoft.SharePoint.Publishing.WebControls.RichImageField);
                    FileInfo fi = new FileInfo(riftype.Module.FullyQualifiedName);
                    File.Copy(riftype.Module.FullyQualifiedName, settings.Path + "\\bin\\" + fi.Name);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Trace.WriteLine("Failed to copy Microsoft.SharePoint.Publishing.dll to the webapplication' bin folder\r\n"+ex.ToString());
                }
                #endregion

                #region Write the RichImageFieldControlAdapter.browser to the local webapplication folder
                string basePath = settings.Path.ToString();
                string destPath = Path.Combine(basePath, "App_Browsers");
                System.IO.FileStream file = new System.IO.FileStream(Path.Combine(destPath, "RichImageFieldControlAdapter.browser"), FileMode.Create , FileAccess.Write);
                System.IO.StreamWriter writer = new System.IO.StreamWriter(file);
                System.IO.Stream resource = this.GetType().Assembly.GetManifestResourceStream("RichImageField_ControlAdapter.FeatureCode.RichImageFieldControlAdapter.browser");
                System.IO.StreamReader reader = new System.IO.StreamReader(resource);
                string s = reader.ReadToEnd();
                s = s.Replace("{6F1D68EA-D81F-4fb4-B360-16D1B0BB879F}", typeof(RichImageFieldControlAdapter).FullName + ", " + typeof(RichImageFieldControlAdapter).Assembly.FullName);
                writer.Write(s);
                writer.Flush();
                writer.Close();
                reader.Close();
                #endregion

                // Touch .browser-files to force activation of the control adapter
                foreach (string filename in System.IO.Directory.GetFiles(destPath, "*.browser"))
                {
                    System.IO.File.SetLastWriteTime(filename, DateTime.Now);
                }
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            if (site == null) return;
            SPWebApplication webApplication = site.WebApplication;

            foreach (SPUrlZone zone in webApplication.IisSettings.Keys)
            {
                // The settings of the IIS application to update
                SPIisSettings settings = webApplication.IisSettings[zone];

                string basePath = settings.Path.ToString();
                string destPath = Path.Combine(basePath, "App_Browsers");

                if (System.IO.File.Exists(Path.Combine(destPath, "RichImageFieldControlAdapter.browser")))
                {
                    System.IO.File.Delete(Path.Combine(destPath, "RichImageFieldControlAdapter.browser"));
                }

                // Touch .browser-files to force de-activation of the control adapter
                foreach (string filename in System.IO.Directory.GetFiles(destPath, "*.browser"))
                {
                    System.IO.File.SetLastWriteTime(filename, DateTime.Now);
                }

            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {

        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {

        }
    }
}
